﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.HomeServer.Controls;
using RouterControl.IO;
using RouterControl.IO.UPnP;
using RouterControl.Web;
using RouterControl.Whs.Wizards;
using Microsoft.HomeServer.Extensibility;
using System.Drawing;
using Microsoft.HomeServer.Common.Client;

namespace RouterControl.WhsAddIn.Wizards.SelectRouter
{
    internal class WizardMain : Wizard
    {
        private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof(WizardMain));

        public IEnumerable<IUPnPDevice> FoundDevices
        {
            get;
            set;
        }

        public IUPnPDevice SelectedDevice
        {
            get;
            set;
        }

        public UsageInfo UsageInfo
        {
            get;
            private set;
        }

        public bool AllowUsageInfoUpload
        {
            get; 
            set;
        }

        public RouterControlSettingsTab2.SettingsCache Settings
        {
            get;
            private set;
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            // upload anything?
            if (this.AllowUsageInfoUpload)
                UploadUsageInfo();

            // continue
            base.OnClosing(e);
        }

        private void UploadUsageInfo()
        {
            if (_log.IsDebugEnabled)
                _log.Debug("Uploading usage information...");

            try
            {
                using (new SimpleUserWaitDialog(Resources.txtUploadingInformation))
                {
                    var sf = new RouterControlWS2Factory();

                    sf.Channel.UploadUsageInfo(this.UsageInfo);
                }

                _log.Info("Uploaded usage information");
            }
            catch (Exception ex)
            {
                _log.Error("Failed to upload usage information", ex);

                throw;
            }
        }

        static WizardMain()
        {
            // install licenses
            Xceed.FileSystem.Licenser.LicenseKey = Microsoft.HomeServer.Common.Licensing.XceedZipLibraryLicenseKey;
            Xceed.Zip.Licenser.LicenseKey = Microsoft.HomeServer.Common.Licensing.XceedZipLibraryLicenseKeyEx;
        }

        public WizardMain(RouterControlSettingsTab2.SettingsCache settings)
        {
            if (settings == null)
                throw new ArgumentNullException("settings");

            // continue
            this.Settings = settings;

            this.Text = string.Format(
                Resources.txtWizardTitle,
                RouterControlVersion.VersionString
                );

            this.Width = CommonSettings.BigWizardStdWidth;
            this.Height = CommonSettings.BigWizardStdHeight;
            this.WizardIcon = Properties.Resources.icoLogo;
            this.UsageInfo = new UsageInfo();

            Init();

            this.SetTopLevel(true);
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            SetTypes(new[] {
               typeof(WizardPageHost<PageWelcome>),
               typeof(WizardPageHost<PageAllowUpload>),
               typeof(WizardPageHost<PageSearch>),
               typeof(WizardPageHost<PageFeatureTest>),
               typeof(WizardPageHost<PageRouterList>),
           });
        }
    }
}